package de.tudresden.inf.ggp.basicplayer;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.eclipse.palamedes.gdl.core.model.IMove;

public class CDataState extends CListAction{
	
	List<IMove> cacs=new LinkedList<IMove>();//add the actions, which have NVist=0 
	
	public CDataState(IMove[] ActArray){
		this.addNewActions(ActArray);
	}
	
	public IMove chooseAction1(int C){
		cacs.clear();
		//case 1.
		Iterator<CAction> it1=super.getListAction().iterator();
		while(it1.hasNext()){
			CAction cac=it1.next();
			if(cac.getNVisit()==0){
				cacs.add(cac.getActionID());
			}
		}
		if(cacs.size()!=0){
			Random random=new Random();
			return cacs.get(random.nextInt(cacs.size()));
		}
		//case 2.
		Iterator<CAction> it2=super.getListAction().iterator();
		double result=0;
		int index=0;
		while(it2.hasNext()){
			CAction cac=it2.next();
			double temp=cac.getAvgValue()+C*Math.sqrt(Math.log(this.getNVisitList())/cac.getNVisit());
			if(result<temp){
				result=temp;
				index=this.getListAction().indexOf(cac);
			}
		}
		return ((CAction) this.getListAction().get(index)).getActionID();
	}
	
	public IMove chooseAction2(CListAction ListActionHistory,int C){
		cacs.clear();
		//case 1.
		Iterator<CAction> it1=super.getListAction().iterator();
		while(it1.hasNext()){
			CAction cac=it1.next();
			if(cac.getNVisit()==0&&!ListActionHistory.IsExist(cac.getActionID())){
				cacs.add(cac.getActionID());
			}
		}
		if(cacs.size()!=0){
			Random random=new Random();
			return cacs.get(random.nextInt(cacs.size()));
		}
		//case 2.
		Iterator<CAction> it2=super.getListAction().iterator();
		double result=0;
		int index=0;
		while(it2.hasNext()){
			CAction cac=it2.next();
			double temp=0;
			if(cac.getNVisit()==0&&ListActionHistory.IsExist(cac.getActionID())){
				temp=ListActionHistory.getAvgValueAction(cac.getActionID());
			}else{
				temp=cac.getAvgValue()+C*Math.sqrt(Math.log(this.getNVisitList())/cac.getNVisit());
			}
			if(result<temp){
				result=temp;
				index=this.getListAction().indexOf(cac);
			}
		}
		return ((CAction) this.getListAction().get(index)).getActionID();
	}
}
/**
 * 3. Class CDataState inheritted from CListAction 
Add two new methods: 
o  Public IMove chooseAction1(C: integer) 
  Case 1: If there are actions that have NVisit==0, choose randomly among them, 
else 
  Case 2: If all actions have NVisit>0, evaluate the equation (1) below and then 
choose the action that have the maximum value. Here is the equation (1): 
Ev = (AvgValue) + C * ( sqrt( ln(getNVisitListAction)/NVisit ) )  (1) 
Where:  
  AvgValue is the average value of the action  CAction.getAvgValue 
  getNVisitListAction is sum of NVisit for all actions  CActionList.getNVisitList 
  NVisit is NVisit of the action  CAction.getNVisit 
o  Public IMove chooseAction2(ListActionHistory: CListAction, C:integer) 
  Case 1: If there are actions that have NVisit=0 and the action does not exist in 
ListActionHistory, choose randomly among them, else 
  Case 2:  
•  For actions that have NVisit=0 and exist in ListActionHistory, then its ev = 
its average value (CAction.getAvgValue).  
•  For action that Nvisit>0, evaluate ev using equation (1).  
•  Choose the maximum value among them. 
 /
 */